<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        .phone {
            width: 300px;
            height: 500px;
            border: 1px solid #333;
            margin: 100px auto;
            position: relative;
        }

        .slideBar {
            width: 100%;
            height: 40px;
            position: absolute;
            top: 60%;
            left: 0;
            background-color: darkgrey;
        }

        .ball {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: pink;
            position: absolute;
            left: 0px;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="phone">
        <div class="slideBar">
            <div class="ball"></div>
        </div>
    </div>
</body>
<script>
    // 手机滑动验证:
    // 1. 拖拽小球 ball 移动
    // 2. 移动过程中 => 如果到达最右边就解锁
    // 3. 如果松开鼠标时 如果没有到达最右边 => 解锁失败 返回原位置

    var phone = document.getElementsByClassName("phone")[0];
    var slideBar = document.getElementsByClassName("slideBar")[0];
    var ball = document.getElementsByClassName("ball")[0];

    var maxLeft = slideBar.clientWidth - ball.clientWidth;

    ball.onmousedown = function (e) {
        var e = e || window.event;
        var posX = e.offsetX;
        console.log(posX);

        document.onmousemove = function (e) {
            var e = e || window.event;
            var x = e.pageX - phone.offsetLeft - posX;

            x = Math.min(Math.max(0, x), maxLeft);

            ball.style.left = x + "px";

            // 移动过程中 => 如果到达最右边就解锁
            if (x >= maxLeft) {
                location.href = "https://www.baidu.com";
                document.onmousemove = null;
            }
        }
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;

            if (ball.offsetLeft < maxLeft) {
                ball.style.left = 0 + "px";
            }
        }

    }



</script>

</html>